﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using PPI.Entities.Enumerator;
using PPI.General;

namespace PPI.Entities.XML
{
    public class EXmlOutboxBase : ICloneable
    {
        #region Protected

        protected string m_XmlFileName;
        protected DatabaseTypes m_DatabaseType;
        protected string m_OutboxPath;
        protected string m_SentPath;
        #endregion

        #region Private

        private string m_Id;
        private bool m_IsDataUploaded;
        private DateTime m_DataFinishedToUpload;
        private bool m_IsImagesUploaded;
        private DateTime m_ImagesFinishedToUpload;
        private string m_FullPath;
        private string m_ConnectionString;
        private bool m_IsOpened;
        private DateTime m_Created;




        #endregion

        #region Properties

        /// <summary>
        /// get the obsolute outbox path.
        /// </summary>
        public string OutboxPath
        {
            get { return m_OutboxPath; }
        }


        /// <summary>
        /// get the obsolute sent path.
        /// </summary>
        public string SentPath
        {
            get { return m_SentPath; }
        }

        /// <summary>
        /// get or set Id of the database
        /// </summary>
        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        /// <summary>
        /// get true when all data is uploaded.
        /// else get false
        /// 
        /// set true when al database is uploaded
        /// else set false
        /// </summary>
        public bool IsDataUploaded
        {
            get { return m_IsDataUploaded; }
            set { m_IsDataUploaded = value; }
        }

        /// <summary>
        /// get datetime when all data is uploaded
        /// set datetime when finish to upload data
        /// </summary>
        public DateTime DataFinishedToUpload
        {
            get { return m_DataFinishedToUpload; }
            set { m_DataFinishedToUpload = value; }
        }

        /// <summary>
        /// get true when all images uploadeded
        /// else get false
        /// 
        /// set true when all images finish to upload
        /// else set false
        /// </summary>
        public bool IsImagesUploaded
        {
            get { return m_IsImagesUploaded; }
            set { m_IsImagesUploaded = value; }
        }

        /// <summary>
        /// get date when all images uploaded
        /// sets date when all images uplaoded
        /// </summary>
        public DateTime ImagesFinishedToUpload
        {
            get { return m_ImagesFinishedToUpload; }
            set { m_ImagesFinishedToUpload = value; }
        }

        /// <summary>
        /// get absolute path of database
        /// set absolute path of database
        /// </summary>
        public string FullPath
        {
            get { return m_FullPath; }
            set { m_FullPath = value; }
        }

        /// <summary>
        /// get the connection string of database
        /// set the connection string of database
        /// </summary>
        public string ConnectionString
        {
            get { return m_ConnectionString; }
            set { m_ConnectionString = value; }
        }

        /// <summary>
        /// get true when the database was openened
        /// else get false
        /// 
        /// set true when open the database
        /// else set false
        /// </summary>
        public bool IsOpened
        {
            get { return m_IsOpened; }
            set { m_IsOpened = value; }
        }

        /// <summary>
        /// get the absolute path of xml.
        /// Ex:see OutboxGPS.xml
        /// </summary>
        public string XmlFileName
        {
            get { return m_XmlFileName; }
        }

        /// <summary>
        /// get database type. 
        /// See PPI.Entities.Enumerators.DatabaseTypes
        /// </summary>
        public DatabaseTypes DatabaseType
        {
            get { return m_DatabaseType; }
        }

        /// <summary>
        /// get creation date of database
        /// </summary>
        public DateTime Created
        {
            get { return File.GetCreationTime(m_ConnectionString); }
        }

        #endregion

        public EXmlOutboxBase ()
        {
        }

        /// <summary>
        /// create xmlNode 
        /// </summary>
        /// <param name="document"></param>
        /// <param name="folder"></param>
        /// <returns></returns>
        public virtual XmlNode CreateNode(XmlDocument document, DirectoryInfo folder)
        {
            XmlNode _FolderNode = document.CreateElement("Folder");
            XmlAttribute _IdAttribute = document.CreateAttribute("ID");
            _IdAttribute.Value = folder.Name;
            _FolderNode.Attributes.Append(_IdAttribute);

            XmlNode _DataUploadedNode = document.CreateElement("Data_Uploaded");
            _DataUploadedNode.AppendChild(document.CreateTextNode(false.ToString()));
            _FolderNode.AppendChild(_DataUploadedNode);

            XmlNode _DateFinishedDataNode = document.CreateElement("Data_Finished");
            _DateFinishedDataNode.AppendChild(document.CreateTextNode(""));
            _FolderNode.AppendChild(_DateFinishedDataNode);


            XmlNode _ImagesUploadedNode = document.CreateElement("Images_Uploaded");
            _ImagesUploadedNode.AppendChild(document.CreateTextNode(false.ToString()));
            _FolderNode.AppendChild(_ImagesUploadedNode);

            XmlNode _DateFinishedImageNode = document.CreateElement("Images_Finished");
            _DateFinishedImageNode.AppendChild(document.CreateTextNode(""));
            _FolderNode.AppendChild(_DateFinishedImageNode);

            XmlNode _Path = document.CreateElement("Path");
            _Path.AppendChild(document.CreateTextNode(folder.FullName));
            _FolderNode.AppendChild(_Path);

            m_ConnectionString = DirHelper.GetSdfPath(folder.FullName);
            XmlNode _ConnectionString = document.CreateElement("ConnectionString");
            _ConnectionString.AppendChild(document.CreateTextNode(m_ConnectionString));
            _FolderNode.AppendChild(_ConnectionString);

            XmlNode _Opened = document.CreateElement("Open");
            _Opened.AppendChild(document.CreateTextNode(false.ToString()));
            _FolderNode.AppendChild(_Opened);
            return _FolderNode;
        }


        /// <summary>
        /// duplicate current object saving
        /// xmlFileName
        /// DatabaseType
        /// OutboxPath
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            EXmlOutboxBase returnObject = new EXmlOutboxBase();
            returnObject.m_XmlFileName = m_XmlFileName;
            returnObject.m_DatabaseType = m_DatabaseType;
            returnObject.m_OutboxPath = m_OutboxPath;
            returnObject.m_SentPath = m_SentPath;
            return returnObject;
        }

        public override bool Equals(object obj)
        {
            if(obj is EXmlOutboxBase)
            {
                EXmlOutboxBase tmpOutbox = (EXmlOutboxBase) obj;
                return (tmpOutbox.Id == m_Id);
            }else
            {
                return false;
            }
        }
    }
}
